﻿#region

using System.IO;
using System.Text;
using System.Web;
//using log4net;
//using log4net.Config;

#endregion

namespace Core.Utils
{
    /// <summary>
    ///     Framework log service
    ///     - singleton pattern
    ///     Mofifitor: CuongNQ
    ///     Modified Date: 11/01/2010
    /// </summary>
    public sealed class LogService
    {
        #region privates

        private const string LogSectionName = "_NVTL_";
        private static readonly LogService Instance = new LogService();
        //private readonly Log4NetWrapLogger iLog;

        #endregion

        private LogService()
        {
            ConfigureLogging();
            //iLog = new Log4NetWrapLogger(LogManager.GetLogger(logSectionName));
        }

        #region business
        private static void WriteLog(string msg)
        {
            //string path = Server.MapPath("~/Time_visible.txt");
            //if (File.Exists(path) == false)
            //    File.CreateText(path);
            //// FileStream fs=new FileStream (path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
            //var ws = new StreamWriter(path);
            //try
            //{
            //    ws.WriteLine(time);
            //    //fs.Close();
            //    ws.Close();
            //}
            //catch { }
        }
        /// <summary>
        ///     Return logger its log data to file
        /// </summary>
        /// <returns></returns>
        //public static ILog Service
        //{
        //    get { return instance.iLog; }
        //}

        private static void ConfigureLogging()
        {
            var configSource = new StringBuilder();
            configSource.Append("<log4net>");
            configSource.Append("  <appender name=\"RollingFile\" type=\"log4net.Appender.RollingFileAppender\">");
            configSource.Append("    <param name=\"File\" value=\"_NVTL_\" />");
            configSource.Append("    <param name=\"AppendToFile\" value=\"true\" />");
            configSource.Append("    <param name=\"MaxSizeRollBackups\" value=\"10\" />");
            configSource.Append("    <param name=\"MaximumFileSize\" value=\"1024KB\" />");
            configSource.Append("    <param name=\"RollingStyle\" value=\"Composite\" />");
            configSource.Append("    <param name=\"DatePattern\" value=\"yyyy-MM-dd.\\lo\\g\" />");
            configSource.Append("    <param name=\"StaticLogFileName\" value=\"false\" />");

            configSource.Append("    <lockingModel type=\"log4net.Appender.FileAppender+MinimalLock\" />");
            configSource.Append("    <layout type=\"log4net.Layout.PatternLayout\">");
            configSource.Append(
                "      <param name=\"ConversionPattern\" value=\"%d{HH:mm:ss} %-5p [%logger] %m%newline\" />");
            configSource.Append("    </layout>");
            configSource.Append("  </appender>");
            configSource.Append("  <root>");
            configSource.Append("    <level value=\"ALL\" />");
            configSource.Append("    <appender-ref ref=\"RollingFile\" />");
            configSource.Append("  </root>");
            configSource.Append("</log4net>");

            var output = new MemoryStream();
            var writer = new StreamWriter(output);
            writer.Write(configSource);
            writer.Flush();
            output.Position = 0;
            //XmlConfigurator.Configure(output);
            output.Close();
        }

        #endregion
    }

    /// <summary>
    ///     Wrapped ILog
    /// </summary>
    //internal class Log4NetWrapLogger : ILog
    //{
    //    //private readonly log4net.ILog log4Net;

    //    //public Log4NetWrapLogger(log4net.ILog log4Net)
    //    //{
    //    //    this.log4Net = log4Net;
    //    //}

    //    //#region ILog Members

    //    ///// <summary>
    //    /////     Log to file with type Debug
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Debug(object message)
    //    //{
    //    //    log4Net.Debug(BuildMessage(message));
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Debug
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    ///// s
    //    //public void Debug(object message, Exception exception)
    //    //{
    //    //    log4Net.Debug(BuildMessage(message), exception);
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Info
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Info(object message)
    //    //{
    //    //    log4Net.Info(BuildMessage(message));
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Info
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Info(object message, Exception exception)
    //    //{
    //    //    log4Net.Info(BuildMessage(message), exception);
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Warn
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Warn(object message)
    //    //{
    //    //    log4Net.Warn(BuildMessage(message));
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Warn
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Warn(object message, Exception exception)
    //    //{
    //    //    log4Net.Warn(BuildMessage(message), exception);
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Error
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Error(object message)
    //    //{
    //    //    log4Net.Error(BuildMessage(message));
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Error
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Error(object message, Exception exception)
    //    //{
    //    //    log4Net.Error(BuildMessage(message), exception);
    //    //}

    //    ///// <summary>
    //    /////     Log to file with type Error
    //    ///// </summary>
    //    ///// <param name="message"></param>
    //    //public void Error( Exception exception)
    //    //{
    //    //    log4Net.Error(exception);
    //    //}

    //    ///// <summary>
    //    /////     Return log with type Debug is enabled
    //    ///// </summary>
    //    //public bool IsDebugEnabled
    //    //{
    //    //    get { return log4Net.IsDebugEnabled; }
    //    //}

    //    ///// <summary>
    //    /////     Return log with type Info is enabled
    //    ///// </summary>
    //    //public bool IsInfoEnabled
    //    //{
    //    //    get { return log4Net.IsInfoEnabled; }
    //    //}

    //    ///// <summary>
    //    /////     Return log with type Warn is enabled
    //    ///// </summary>
    //    //public bool IsWarnEnabled
    //    //{
    //    //    get { return log4Net.IsWarnEnabled; }
    //    //}

    //    ///// <summary>
    //    /////     Return log with type Error is enabled
    //    ///// </summary>
    //    //public bool IsErrorEnabled
    //    //{
    //    //    get { return log4Net.IsErrorEnabled; }
    //    //}

    //    //#endregion

    //    private static string BuildMessage(object message)
    //    {
    //        var aboveFrame = new StackFrame(2);
    //        return
    //            String.Format("[Class: {0} - Method: {1}] - {2} ", aboveFrame.GetMethod().ReflectedType.FullName,
    //                aboveFrame.GetMethod().Name, message);
    //    }
    //}
}